package com.arsn.myf.resource.controller;

import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.resource.entity.Oss;
import com.arsn.myf.resource.service.OssService;
import com.arsn.myf.resource.util.MinioUtil;
import com.arsn.myf.resource.vo.OssVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wildfly.common.annotation.NotNull;
import java.util.Arrays;

/**
 * 存储对象控制层
 * @author zhongrj
 * @since 2022-10-11
 */
@RestController
@RequestMapping("/oss")
@Api(value = "存储对象", tags = "存储对象")
public class OssController {

    @Autowired
    private MinioUtil minioUtil;
    
    @Autowired
    private OssService ossService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public R upload(@RequestParam(value = "file") MultipartFile file){
        //上传并返回
        return R.data(minioUtil.uploadByFile(file));
    }
    
    
    /**
     * 查询存储对象的分页数据
     * @param page
     * @param oss
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询存储对象的分页数据")
    public R list(Page<Oss> page, Oss oss){
        return R.data(ossService.page(page,new QueryWrapper<>(oss)));
    }


    /**
     * 自定义查询存储对象的分页数据
     * @param page
     * @param oss
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义查询存储对象的分页数据")
    public R page(Page<OssVO> page, OssVO oss){
        return R.data(ossService.selectPageOssList(page,oss));
    }

    /**
     * 存储对象详情信息
     * @param oss
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "存储对象详情信息")
    public R detail(Oss oss){
        return R.data(ossService.getOne(new QueryWrapper<>(oss)));
    }


    /**
     * 存储对象新增
     * @param oss
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "存储对象新增")
    public R save(@RequestBody Oss oss){
        return R.status(ossService.save(oss));
    }

    /**
     * 存储对象修改
     * @param oss
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "存储对象修改")
    public R update(@RequestBody Oss oss){
        return R.status(ossService.updateById(oss));
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "批量删除")
    public R remove(@NotNull String ids){
        return R.status(ossService.removeByIds(Func.stringToLong(Arrays.asList(ids.split(",")))));
    }

}
